<?php
namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\JoinApplication as JoinApplicationModel;
use think\Exception;
use think\exception\ValidateException;


class JoinApplication extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    public function individual()
    {
        if ($this->request->isOptions()) {
            exit();
        }

        $payload = [
            'type' => 0,
            'ind_name' => $this->request->param('username'),
            'ind_id_no' => $this->request->param('idAccount'),
            'ind_id_front_image' => $this->request->param('idPhotoFront'),
            'ind_id_rear_image' => $this->request->param('idPhotoRear'),
            'ind_tel' => $this->request->param('tel'),
            'ind_bank_account' => $this->request->param('bankAccount'),
            'ind_bank_tel' => $this->request->param('telForBankAccount'),
            'manager_name' => $this->request->param('managerUsername'),
            'manager_tel' => $this->request->param('managerTel'),
            'manager_id_front_image' => $this->request->param('idPhotoFrontAdmin'),
            'manager_id_rear_image' => $this->request->param('idPhotoRearAdmin'),
            'biz_category' => $this->request->param('bizCategory'),
            'biz_license_image' => $this->request->param('bizLicense'),
            'tm_type' => $this->request->param('trademarkType'),
            'tm_reg_no' => $this->request->param('trademarkRegistrationNo'),
            'tm_reg_cert_image' => $this->request->param('trademarkRegistrationCert'),
            'tm_reg_valid_time' => $this->request->param('trademarkRegistrationCertValidityPeriod'),
            'tm_auth_cert_image' => $this->request->param('trademarkAuthorizationCert'),
            'tm_auth_valid_time' => $this->request->param('trademarkAuthorizationValidityPeriod'),
        ];

        try {
            $this->validateFailException(true)
                ->validate($payload, 'JoinApplication.individual');
        } catch (ValidateException $e) {
            $this->error($e->getMessage(), null, 400);
        }

        try {
            $application_record = (new JoinApplicationModel($payload))->allowField(true)->save();
        } catch (Exception $e) {
            $this->error('网络错误，请稍后再试', null, 500);
        }
        $this->success('提交成功', $application_record);
    }

    public function enterprise()
    {
        if ($this->request->isOptions()) {
            exit();
        }

        $payload = [
            'type' => 1,
            'ent_name' => $this->request->param('companyName'),
            'ent_addr' => $this->request->param('companyAddress'),
            'biz_category' => $this->request->param('bizCategory'),
            'ent_repr_name' => $this->request->param('legalRepresentativeName'),
            'ent_repr_id_no' => $this->request->param('legalRepresentativeIdAccount'),
            'ent_repr_id_front_image' => $this->request->param('legalRepresentativeIdPhotoFront'),
            'ent_repr_id_rear_image' => $this->request->param('legalRepresentativeIdPhotoRear'),
            'ent_repr_tel' => $this->request->param('legalRepresentativeTel'),
            'biz_license_image' => $this->request->param('bizLicense'),
            'ent_company_code' => $this->request->param('companyCode'),
            'ent_bank_account' => $this->request->param('companyBankAccount'),
            'ent_bank_permits_image' => $this->request->param('bankPermits'),
            'manager_name' => $this->request->param('managerUsername'),
            'manager_tel' => $this->request->param('managerTel'),
            'manager_id_front_image' => $this->request->param('idPhotoFrontAdmin'),
            'manager_id_rear_image' => $this->request->param('idPhotoRearAdmin'),
            'tm_type' => $this->request->param('trademarkType'),
            'tm_reg_no' => $this->request->param('trademarkRegistrationNo'),
            'tm_reg_cert_image' => $this->request->param('trademarkRegistrationCert'),
            'tm_reg_valid_time' => $this->request->param('trademarkRegistrationCertValidityPeriod'),
            'tm_auth_cert_image' => $this->request->param('trademarkAuthorizationCert'),
            'tm_auth_valid_time' => $this->request->param('trademarkAuthorizationValidityPeriod'),
        ];

        try {
            $this->validateFailException(true)
                ->validate($payload, 'JoinApplication.enterprise');
        } catch (ValidateException $e) {
            $this->error($e->getMessage(), null, 400);
        }

        try {
            $application_record = (new JoinApplicationModel($payload))->allowField(true)->save();
        } catch (Exception $e) {
            $this->error('网络错误，请稍后再试', null, 500);
        }
        $this->success('提交成功', $application_record);
    }

    public function query()
    {
        if ($this->request->isOptions()) {
            exit();
        }

        $id = $this->request->param('id');
        if (empty($id)) {
            $this->error('请输入您要查询的身份证号', null, 400);
        }

        $count = JoinApplicationModel::where('ind_id_no|ent_repr_id_no', $id)->count();
        $entries = JoinApplicationModel::where('ind_id_no|ent_repr_id_no', $id)
            ->order('createtime desc')
            ->select();

        $ret = [
            'entries' => collection($entries)->toArray(),
            'count' => $count,
            'domain' => $this->request->domain(),
        ];
        $this->success('返回成功', $ret);
    }
}